/**
 * 行编辑扩展。
 */
Ext.define("ES.view.RowEditingEx",{
    //基类。
    extend:"Ext.grid.plugin.Editing",
    //别名。
    alias:"plugin.roweditingex",
    //依赖。
    requires:["Ext.grid.RowEditor"],
    //编辑样式。
    editStyle:"row",
    //是否自动取消。
    autoCancel: true,
    //是否显示错误摘要。
    errorSummary: true,
    //构造函数。
    constructor: function() {
        var me = this;
        me.callParent(arguments);

        if (!me.clicksToMoveEditor) {
            me.clicksToMoveEditor = me.clicksToEdit;
        }
        me.autoCancel = !!me.autoCancel;
    },
    //初始化。
    init: function(grid) {
        this.callParent([grid]);
    },
    //销毁。
    destroy: function() {
        var me = this;
        Ext.destroy(me.editor);
        me.callParent(arguments);
    },
    //开始编辑。
    startEdit: function(record, columnHeader) {
        var me = this,
            editor = me.getEditor();
        if ((me.callParent(arguments) !== false) && (editor.beforeEdit() !== false)) {
            editor.startEdit(me.context.record, me.context.column);
            return true;
        }
        return false;
    },
    //取消编辑。
    cancelEdit: function() {
        var me = this;
        if (me.editing) {
            me.getEditor().cancelEdit();
            me.callParent(arguments);
        }
    },
    //完成编辑。
    completeEdit: function() {
        var me = this;
        if (me.editing && me.validateEdit()) {
            me.editing = false;
            me.fireEvent('edit', me, me.context);
            var record =me.context.me.context;
            record.commit();
        }
    },
    //验证编辑。
    validateEdit: function() {
        var me             = this,
            editor         = me.editor,
            context        = me.context,
            record         = context.record,
            newValues      = {},
            originalValues = {},
            editors        = editor.items.items,
            e,
            eLen           = editors.length,
            name, item;

        for (e = 0; e < eLen; e++) {
            item = editors[e];
            name = item.name;

            newValues[name]      = item.getValue();
            originalValues[name] = record.get(name);
        }

        Ext.apply(context, {
            newValues      : newValues,
            originalValues : originalValues
        });

        return me.callParent(arguments) && me.getEditor().completeEdit();
    },
    //获取编辑。
    getEditor: function() {
        var me = this;

        if (!me.editor) {
            me.editor = me.initEditor();
        }
        return me.editor;
    },
    //初始化编辑。
    initEditor: function() {
        var me       = this,
            grid     = me.grid,
            view     = me.view,
            headerCt = grid.headerCt,
            cfg      = {
                saveBtnText:"保存",
                cancelBtnText:"取消",
                deleteBtnText:"删除",
                errorsText:"错误信息",
                autoCancel: me.autoCancel,
                errorSummary: me.errorSummary,
                fields: headerCt.getGridColumns(),
                hidden: true,
                deleteRow:function(){
                    var me = this;  
                    var store = me.context.store;
                    var model = me.context.record;
                    store.remove(model);
                    //me.fireEvent("deleterow",me.context.record);
                    me.hide();
                    return true;
                },
                // keep a reference..
                editingPlugin: me,
                renderTo: view.el,
                getFloatingButtons: this.buildButtons
            }
        return Ext.create('Ext.grid.RowEditor', cfg);
    },
    //构建按钮。
    buildButtons:function(){
        var me = this,
            cssPrefix = Ext.baseCSSPrefix,
            btnsCss = cssPrefix + "grid-row-editor-buttons",
            plugin = me,
            btns;

        if (!me.floatingButtons) {
            btns = me.floatingButtons = new Ext.Container({
                renderTpl: [
                    '<div class="{baseCls}-ml"></div>',
                    '<div class="{baseCls}-mr"></div>',
                    '<div class="{baseCls}-bl"></div>',
                    '<div class="{baseCls}-br"></div>',
                    '<div class="{baseCls}-bc"></div>',
                    '{%this.renderContainer(out,values)%}'
                ],
                width: 240,
                renderTo: plugin.el,
                baseCls: btnsCss,
                layout: {
                    type: 'hbox',
                    align: 'middle'
                },
                defaults: {
                    flex: 1,
                    margins: '0 1 0 1'
                },
                items: [{
                    itemId: 'update',
                    xtype: 'button',
                    handler: plugin.completeEdit,
                    scope: plugin,
                    text: plugin.saveBtnText,
                    disabled: !plugin.isValid,
                    minWidth: Ext.panel.Panel.prototype.minButtonWidth
                }, {
                    xtype: 'button',
                    handler: plugin.cancelEdit,
                    scope: plugin,
                    text: plugin.cancelBtnText,
                    minWidth: Ext.panel.Panel.prototype.minButtonWidth
                },{
                    xtype: 'button',
                    handler:plugin.deleteRow,
                    scope: plugin,
                    text: plugin.deleteBtnText,
                    minWidth: Ext.panel.Panel.prototype.minButtonWidth
                }]
            });
            plugin.mon(btns.el, {
                mousedown: Ext.emptyFn,
                click: Ext.emptyFn,
                stopEvent: true
            });
        }
        return plugin.floatingButtons;
    },
    //初始化编辑触发器。
    initEditTriggers: function() {
        var me = this,
            moveEditorEvent = me.clicksToMoveEditor === 1 ? 'click' : 'dblclick';

        me.callParent(arguments);

        if (me.clicksToMoveEditor !== me.clicksToEdit) {
            me.mon(me.view, 'cell' + moveEditorEvent, me.moveEditorByClick, me);
        }
    },
    //添加头事件。
    addHeaderEvents: function(){
        var me = this;
        me.callParent();
        
        me.mon(me.grid.headerCt, {
            scope: me,
            columnresize: me.onColumnResize,
            columnhide: me.onColumnHide,
            columnshow: me.onColumnShow,
            columnmove: me.onColumnMove
        });
    },
    //
    startEditByClick: function() {
        var me = this;
        if (!me.editing || me.clicksToMoveEditor === me.clicksToEdit) {
            me.callParent(arguments);
        }
    },
    //
    moveEditorByClick: function() {
        var me = this;
        if (me.editing) {
            me.superclass.onCellClick.apply(me, arguments);
        }
    },
    //添加列。
    onColumnAdd: function(ct, column) {
        if (column.isHeader) {
            var me = this,
                editor;

            me.initFieldAccessors(column);

            // Only inform the editor about a new column if the editor has already been instantiated,
            // so do not use getEditor which instantiates the editor if not present.
            editor = me.editor;
            if (editor && editor.onColumnAdd) {
                editor.onColumnAdd(column);
            }
        }
    },
    //列移除。
    onColumnRemove: function(ct, column) {
        if (column.isHeader) {
            var me = this,
                editor = me.getEditor();

            if (editor && editor.onColumnRemove) {
                editor.onColumnRemove(column);
            }
            me.removeFieldAccessors(column);
        }
    },
    // 列尺寸。
    onColumnResize: function(ct, column, width) {
        if (column.isHeader) {
            var me = this,
                editor = me.getEditor();

            if (editor && editor.onColumnResize) {
                editor.onColumnResize(column, width);
            }
        }
    },
    //隐藏列。
    onColumnHide: function(ct, column) {
        // no isHeader check here since its already a columnhide event.
        var me = this,
            editor = me.getEditor();

        if (editor && editor.onColumnHide) {
            editor.onColumnHide(column);
        }
    },
    // 显示列。
    onColumnShow: function(ct, column) {
        // no isHeader check here since its already a columnshow event.
        var me = this,
            editor = me.getEditor();

        if (editor && editor.onColumnShow) {
            editor.onColumnShow(column);
        }
    },
    // 移动列。
    onColumnMove: function(ct, column, fromIdx, toIdx) {
        // no isHeader check here since its already a columnmove event.
        var me = this,
            editor = me.getEditor();

        if (editor && editor.onColumnMove) {
            editor.onColumnMove(column, fromIdx, toIdx);
        }
    },
    //列字段。 
    setColumnField: function(column, field) {
        var me = this,
            editor = me.getEditor();
            
        editor.removeField(column);
        me.callParent(arguments);
        me.getEditor().setField(column);
    }
})

